package ${groupId}.${artifactIdProjectPackageName}.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.util.AntPathMatcher;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {

    <#noparse>

    @Value("${swagger.domain}")
    private String swaggerDomain;

    @Value("${swagger.name}")
    private String swaggerName;

    @Value("${swagger.main-url}")
    private String swaggerMainUrl;

    </#noparse>

    @Bean
    public Docket docketStatistic() {
        return new Docket(DocumentationType.SWAGGER_2).groupName( swaggerName + "接口")
                .apiInfo(apiInfoStatistic())
                .select()
                .apis(RequestHandlerSelectors.basePackage("${groupId}.${artifactIdProjectPackageName}.controller"))
                .paths(input -> {
                    AntPathMatcher matcher = new AntPathMatcher();
                    return matcher.match("/**/**", input);
                })
                .build();
    }

    private ApiInfo apiInfoStatistic() {
        return new ApiInfoBuilder()
                .title(swaggerName + "接口文档")
                .description(swaggerName + "接口文档")
                .termsOfServiceUrl(swaggerMainUrl)
                .version("1.0.0")
                .build();
    }

}
